package class03;

import class02.randoom.GenerateRandomArray;

import java.util.Arrays;

/**
 * @Auther: xw
 * @Create: 2022/3/4 17:13
 * @Description: 找到小于等于x的最右边的位置
 */
public class FindRightIndex {

    public static int indexOfTheRight(int[] arr, int target){
        if(null == arr || arr.length<0)
            return -1;
        int left = 0;
        int right = arr.length;
        int ans= -1;
        while(left <= right){
            int middle = (left + right)/2;
            if(arr[middle] <= target){
                ans = middle;
                left = middle +1;
            }else{
                right = middle-1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        //获取随机数组
        int[] array1 = GenerateRandomArray.getArray1(100, 12);
        Arrays.sort(array1);
        GenerateRandomArray.printArray(array1);
        System.out.println(indexOfTheRight(array1,1));

    }
}
